package com.example.model.api;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

@JsonInclude(JsonInclude.Include.NON_NULL) // 仅当属性非null时才序列化
@Schema(description = "通用响应结果", type = "object")
public class Result<T> implements Serializable {

  private static final long serialVersionUID = -6149948941889889657L;

  @Schema(description = "状态码: HttpStatus枚举", example = "200")
  private Integer code;

  @Schema(description = "提示信息", example = "操作成功")
  private String message;

  @Schema(description = "数据结果", implementation = Object.class) // 使用Object作为默认实现类型
  private T data;

  public static final Integer SUCCESS_CODE = 200;
  public static final String SUCCESS_OK = "ok";
  public static final Integer FAIL_CODE = HttpStatus.BAD_REQUEST.value();
  public static final String FAIL_MESSAGE = "fail";

  public static <T> Result<T> success() {
    return ResultBuilder.<T>aResult()
            .data(null)
            .code(SUCCESS_CODE)
            .msg(SUCCESS_OK)
            .build();
  }
  public static Result<String> ok() {
    return ResultBuilder.<String>aResult()
            .data(null)
            .code(SUCCESS_CODE)
            .msg(SUCCESS_OK)
            .build();
  }

  public static <T> Result<T> success(String message) {
    return ResultBuilder.<T>aResult()
            .data(null)
            .code(SUCCESS_CODE)
            .msg(message)
            .build();
  }
  public static <T> Result<T> error(String message) {
    return ResultBuilder.<T>aResult()
            .data(null)
            .code(FAIL_CODE)
            .msg(message)
            .build();
  }


  public Result<T> ok(T data) {
    this.setData(data).setCode(SUCCESS_CODE).setMessage(SUCCESS_OK);
    return this;
  }
  public Result<T> unauthorizedError(T data) {
    this.setData(data).setCode(HttpStatus.UNAUTHORIZED.value()).setMessage(FAIL_MESSAGE);
    return this;
  }
  public Result<T> error(T data) {
    this.setData(data).setCode(FAIL_CODE).setMessage(FAIL_MESSAGE);
    return this;
  }
  public static <T> Result<T> data(T data) {
    return ResultBuilder.<T>aResult()
            .data(data)
            .code(SUCCESS_CODE)
            .msg(SUCCESS_OK)
            .build();
  }

  public static <T> Result<T> fail(T data) {
    return ResultBuilder.<T>aResult()
            .data(data)
            .code(FAIL_CODE)
            .msg(FAIL_MESSAGE)
            .build();
  }

  public static <T> Result<T> data(T data, String responseMessage) {
    return ResultBuilder.<T>aResult()
            .data(data)
            .code(SUCCESS_CODE)
            .msg(responseMessage)
            .build();
  }

  public Result() {}

  public Integer getCode() {
    return code;
  }

  public Result<T> setCode(Integer code) {
    this.code = code;
    return this;
  }

  public String getMessage() {
    return message;
  }

  public Result<T> setMessage(String message) {
    this.message = message;
    return this; // 支持链式调用
  }

  public T getData() {
    return data;
  }

  public Result<T> setData(T data) {
    this.data = data;
    return this;
  }
}